package com.hyy.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyy.springbootblog.common.pojo.dataobject.BlogInfo;
import com.hyy.springbootblog.common.pojo.request.AddBlogParam;
import com.hyy.springbootblog.common.pojo.request.UpBlogParam;
import com.hyy.springbootblog.common.pojo.response.BlogInfoResponse;
import com.hyy.springbootblog.common.utils.BeanConver;
import com.hyy.springbootblog.mapper.BlogInfoMapper;
import com.hyy.springbootblog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {

        //查询数据库
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, 0)
                .orderByDesc(BlogInfo::getId));

        //对象转换
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            return BeanConver.trans(blogInfo);
        }).collect(Collectors.toList());

        return blogInfoResponses;
    }

    /*
    * 获取博客详情*/
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanConver.trans(selectBlogById(blogId));
    }

    @Override
    public Boolean addBlog(AddBlogParam addBlogParam) {
            BlogInfo blogInfo=new BlogInfo();
            BeanUtils.copyProperties(addBlogParam,blogInfo);

            //插入博客
            try{
                int result=blogInfoMapper.insert(blogInfo);
               if(result==1) return true;
            }catch (Exception e){
                log.error("博客插入数据库失败：{}",e.getMessage());
            }
            return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam upBlogParam) {
        BlogInfo blogInfo=BeanConver.trans(upBlogParam);
        int result=-1;
        try{
            result=blogInfoMapper.updateById(blogInfo);
        }catch (Exception e){
            log.error("数据库更新异常{}",e.getMessage());
        }
        if(result==1) return true;
        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {

        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try {
            blogInfoMapper.updateById(blogInfo);
        }catch (Exception e){
            log.error("博客删除失败,e{}",e.getMessage());
        }
        return true;
    }

    public BlogInfo selectBlogById(Integer blogId) {
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));
    }
}
